package view;

import javax.accessibility.AccessibleContext;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JSeparator;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JSeparator;
import javax.swing.table.DefaultTableModel;

import controller.StorageController;

public class StorageTab extends JPanel implements ActionListener{
	
	JButton findButton, createButton, clearButton, updateButton, addButton, removeButton;
	JLabel searchLabel, idLabel, nameLabel, stockLabel, priceLabel, descriptionLabel, minStockLabel, searchException, standardException,stockException;
	JTextField  searchField, idField, nameField, stockField, priceField, minStockField;
	StorageController storageController = StorageController.getInstance();
	JSeparator separator, separator1;
	JTextArea descriptionArea;
	private JScrollPane scrollPane;
	private JTable table;
	private OrderTableModel model;
	JPanel row8;
	
	public StorageTab(){
		
		setLayout(null);
		
		//row1
		JPanel row1 = new JPanel();
		row1.setBounds(0, 0, 1000, 80);
		row1.setLayout(null);
		add(row1);
		
		searchLabel = new JLabel ("Item name or id");
		searchLabel.setBounds(15, 18, 90, 10);
		searchField = new JTextField(15);
		searchField.setBounds(120, 15, 150, 20);
		findButton = new JButton("Find");
		findButton.setBounds(285, 15, 80, 20);
		findButton.addActionListener(this);
		row1.add(searchLabel);
		row1.add(searchField);
		row1.add(findButton);
		//searchException label
		searchException = new JLabel();
		searchException.setBounds(15, 50, 400, 20);
		searchException.setForeground(Color.red);
		row1.add(searchException);
		
		separator = new JSeparator();
		separator.setBounds(0,70, 1000, 10);
		row1.add(separator);
		
		//row 2
		JPanel row2 = new JPanel();
		row2.setBounds(0, 75, 1000, 35); 
		row2.setLayout(null);
		add(row2);
		
		idLabel = new JLabel("ID");
		idLabel.setBounds(15, 18, 60, 10);
		idField = new JTextField(15);
		idField.setBounds(90, 15, 150, 20);
		nameLabel = new JLabel("Name");
		nameLabel.setBounds(250, 15, 60, 20);
		nameField = new JTextField(15);
		nameField.setBounds(290, 15, 150, 20);
		row2.add(idLabel);
		row2.add(idField);
		row2.add(nameLabel);
		row2.add(nameField);
		
		// row3
		JPanel row3 = new JPanel();
		row3.setBounds(0, 100, 1000, 35);
		row3.setLayout(null);
		add(row3);
		
		minStockLabel = new JLabel("Min. Stock");
		minStockLabel.setBounds(15, 18, 60, 10);
		minStockField = new JTextField(15);
		minStockField.setBounds(90, 15, 150, 20);
		priceLabel = new JLabel("Price");
		priceLabel.setBounds(250, 15, 60, 20);
		priceField = new JTextField(15);
		priceField.setBounds(290, 15, 150, 20);
		
		row3.add(minStockLabel);
		row3.add(minStockField);
		row3.add(priceLabel);
		row3.add(priceField);
		
		
		//row4
		JPanel row4 = new JPanel();
		row4.setBounds(0, 125, 1000,45);
		row4.setLayout(null);
		add(row4);
		
		stockLabel = new JLabel("Stock");
		stockLabel.setBounds(15, 15, 100, 20);
		stockField = new JTextField(15);
		stockField.setBounds(90, 15, 150, 20);
		row4.add(stockLabel);
		row4.add(stockField);
		stockException = new JLabel("No more in stock");
		stockException.setBounds(250, 15, 100, 20);
		stockException.setForeground(Color.red);
		stockException.setVisible(false);
		row4.add(stockException);
		
		//row5
		JPanel row5 = new JPanel();
		row5.setBounds(0, 165, 1000, 130);
		row5.setLayout(null);
		add(row5);
		
		descriptionLabel = new JLabel("Description");
		descriptionLabel.setBounds(15, 20, 65, 20 );
		descriptionArea = new JTextArea(4, 50);
		descriptionArea.setLineWrap(true);
		descriptionArea.setBorder(BorderFactory.createLineBorder(Color.gray));
		descriptionArea.setBounds(90, 15, 350, 100);
		row5.add(descriptionLabel);
		row5.add(descriptionArea);
		standardException = new JLabel();
		standardException.setBounds(15, 120, 400, 20);
		standardException.setForeground(Color.red);
		row5.add(standardException);
		
		
		//row6
		JPanel row6 = new JPanel();
		row6.setBounds(0, 290, 600, 70);
		row6.setLayout(null);
		add(row6);
		
		createButton = new JButton("Create");
		createButton.setBounds(15, 15, 80, 20);
		createButton.addActionListener(this);
		updateButton = new JButton("Update");
		updateButton.setBounds(100, 15, 80, 20);
		updateButton.addActionListener(this);
		clearButton = new JButton("Clear");
		clearButton.setBounds(185, 15, 80, 20);
		clearButton.addActionListener(this);
		addButton = new JButton("Add Item");
		addButton.setBounds(270, 15, 100, 20);
		addButton.addActionListener(this);
		removeButton = new JButton("Delete");
		removeButton.setBounds(375, 15, 80, 20);
		removeButton.addActionListener(this);
		
		row6.add(createButton);
		row6.add(updateButton);
		row6.add(clearButton);
		row6.add(addButton);
		row6.add(removeButton);
		
		separator1 = new JSeparator();
		separator1.setBounds(0,60, 1000, 10);
		row6.add(separator1);
	
		row8 = new JPanel();
		row8.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Items"));
		row8.setBounds(10, 360, 560, 160);
		row8.setLayout(new GridLayout(1,0));
		add(row8);
		

		
		//
		model = new OrderTableModel(new ArrayList<Object[]>(), new String[]{"Item Id", "Item name", "Stock", "Price"});
		table = new JTable(model);		
		scrollPane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		row8.add(scrollPane);

	}
		
		@Override
		public void actionPerformed(ActionEvent e){
			if(e.getSource() == createButton){
				System.out.println("Creating item.");
				try{
					clearException();
					createItem();
				} catch(Exception ex) {
					standardException();
				}
			}
			if(e.getSource() == updateButton){
				System.out.println("Saving.");
				try{
					clearException();
					updateItem();
				} catch(Exception ex) {
					standardException();
				}
			}
			if(e.getSource() == findButton){
				System.out.println("Searching.");
				try{
					clearException();
					findItem();
				} catch(Exception ex) {
					searchException();
				}
			}
			if(e.getSource() == clearButton){
				System.out.println("Clearing.");
				clearFields();
			}
			if(e.getSource() == addButton){
				System.out.println("Adding item to Order.");
				try{
					addItemToOrder();
						
					
					findItem();
				} catch(Exception ex) {
					
				}
			}
			if(e.getSource() == removeButton){
				System.out.println("Removing item from Storage");
				removeItem();
				clearFields();
			}
		}

		private void addItemToOrder(){
			/*JPanel jp = (JPanel)this.getParent().getComponent(1);
			JPanel jp2 = (JPanel)jp.getComponent(0);
			jp2.list();
			JTextField jtf = (JTextField)jp2.getComponent(1);
			System.out.println(jtf.getText());
			*/
			//System.out.println(OrderTab.getOrderId());
			try{
				boolean moreinstock =storageController.addItemToOrder(Integer.parseInt(idField.getText()), Integer.parseInt(OrderTab.getOrderId()));
				if(!moreinstock) {
					System.out.println("no more in stock");
					stockException.setVisible(true);
				}
			} catch(Exception ex) {
				System.out.println(Integer.parseInt(idField.getText()) + " " + Integer.parseInt(OrderTab.getOrderId()));
				System.out.println(ex);
				standardException();
			}
		}

		private void createItem(){
			storageController.createItem((Integer.parseInt(idField.getText())),nameField.getText(),
										descriptionArea.getText(),(Integer.parseInt(stockField.getText())),
										(Double.parseDouble(priceField.getText())),(Integer.parseInt(minStockField.getText())));
		}	

		private void findItem(){
			ArrayList<Object[]> al;
			al = storageController.findItems(searchField.getText());
			if(al.size() == 1){
				Object[] obj = al.get(0);
				idField.setText(String.valueOf(obj[0]));
				nameField.setText(String.valueOf(obj[1]));
				descriptionArea.setText(String.valueOf(obj[2]));
				priceField.setText(String.valueOf(obj[3]));
				stockField.setText(String.valueOf(obj[4]));
				minStockField.setText(String.valueOf(obj[5]));
				row8.remove(scrollPane);
				table = new JTable(new OrderTableModel(new ArrayList<Object[]>(), new String[]{"Item Id", "Item name", "Stock", "Price"}));
				scrollPane = new JScrollPane(table);
				table.setFillsViewportHeight(true);
				row8.add(scrollPane);
			} else if(al.size() > 1){
				row8.remove(scrollPane);
				table = new JTable(new OrderTableModel(al, new String[]{"Item Id", "Item name", "Stock", "Price"}));
				scrollPane = new JScrollPane(table);
				table.setFillsViewportHeight(true);
				row8.add(scrollPane);
				clearFields();
			}
		}
	
		private void updateItem(){
			storageController.updateItem(searchField.getText(),(Integer.parseInt(idField.getText())),nameField.getText(),
										descriptionArea.getText(),(Integer.parseInt(stockField.getText())),
										(Double.parseDouble(priceField.getText())),(Integer.parseInt(minStockField.getText())));
		}
		
		private void clearFields(){
			searchField.setText(null);
			idField.setText(null); 
			nameField.setText(null);
			stockField.setText(null);
			priceField.setText(null);
			descriptionArea.setText(null);
			minStockField.setText(null);
			clearException();
		}
		
		public void removeItem(){
			storageController.removeItem(idField.getText());
		}
		
		private void searchException(){
			searchException.setText("Search Error - Try Again!");
		}
		
		private void standardException(){
			standardException.setText("Error! - Please Check The Information.");
		}
		
		private void clearException(){
			searchException.setText(null);
			standardException.setText(null);
		}
}
